﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DatabasesPage.xaml.cs" company="Marcin Jastrzebski">
//   Copyright (c) Marcin Jastrzebski (marcin@marcinj.net)
// </copyright>
// <summary>
//   Page that displays a list of databases.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace mPassRT.Pages
{
    using System;
    using System.ComponentModel;
    using System.Linq;

    using mPassRT.Utilities;
    using mPassRT.ViewModels;
    using mPassRT.ViewModels.Databases;

    using Windows.Foundation;
    using Windows.UI.Popups;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;

    /// <summary>
    /// Page that displays a list of databases.
    /// </summary>
    public sealed partial class DatabasesPage
    {
        #region Constructors and Destructors

        /// <summary>Initializes a new instance of the <see cref="DatabasesPage"/> class.</summary>
        public DatabasesPage()
        {
            this.InitializeComponent();
            this.GetViewModel().PropertyChanged += this.OnViewModelPropertyChanged;
        }

        #endregion

        #region Methods

        /// <summary>Gets the view model.</summary>
        /// <returns>The <see cref="DatabasesViewModel"/>.</returns>
        private DatabasesViewModel GetViewModel()
        {
            return (DatabasesViewModel)this.DataContext;
        }

        /// <summary>Called when an item is clicked.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The event arguments.</param>
        private async void OnDatabasesGridViewItemClick(object sender, ItemClickEventArgs e)
        {
            if (e.ClickedItem is DatabaseViewModel)
            {
                this.GetViewModel().OpenCommand.Execute(e.ClickedItem);
                return;
            }

            var menu = new PopupMenu();
            menu.Commands.Add(
                new UICommand("New Password Patabase", _ => this.GetViewModel().AddNewCommand.Execute(null)));
            menu.Commands.Add(
                new UICommand(
                    "Existing Password Patabase", _ => this.GetViewModel().AddExistingCommand.Execute(null)));

            var gridView = (GridView)sender;
            var addButtonContainer =
                (FrameworkElement)gridView.ItemContainerGenerator.ContainerFromItem(gridView.Items.Last());

            // get rectangle of the + button
            Rect containerRect = addButtonContainer.GetFrameworkElementRectangle();

            // adjust rectangle
            var iconSize = (double)this.Resources["IconSize"];
            var margin = (Thickness)this.Resources["IconMargin"];
            containerRect.Width = iconSize + margin.Left + margin.Right;

            await menu.ShowForSelectionAsync(containerRect, Placement.Right);
        }

        /// <summary>Called when selection changes.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The event arguments.</param>
        private void OnDatabasesGridViewSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var gridView = (GridView)sender;
            NewItemViewModel newItem = e.AddedItems.OfType<NewItemViewModel>().FirstOrDefault();
            if (newItem != null)
            {
                gridView.SelectedItems.Remove(newItem);
                return;
            }

            this.GetViewModel().SelectedDatabases = gridView.SelectedItems.OfType<DatabaseViewModel>().ToList();
        }

        /// <summary>Reacts to changes in some view model properties by opening the app bar.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The event arguments.</param>
        private void OnViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "AreDatabasesSelected" && this.GetViewModel().AreDatabasesSelected)
            {
                this.appBar.IsOpen = true;
            }
        }

        #endregion
    }
}